12.1. Assertion System Output
In AROAPL, ⎕ASSERT system output is the
only way to raise an error.
If a condition is not match, the program terminates
immediately with an error message.
⎕ASSERT ← ⟨⊥, "SAMPLE ERROR MESSAGE"⟩;
ASSERTION ERROR: SAMPLE ERROR MESSAGE
12.2. Errors as Return Values
Also, while many languages often use
try-catch statements for exception
handling, my language takes the same approach as Go,
returning errors as values with using the
⎕IF system function, ⎕SWITCH
system function, or ⎕RETURNIF system
output.
Furthermore, as I’ll discuss in chapter 13, it’s preferable to use fixed-length strings when returning errors. As the result, what we return is more of an error code than an error message. The error code itself explains the outcome of the operation, and if further clarification is needed, that becomes the responsibility of the caller.
It is free to define rules of error codes.
For example, a password-checker can be defined like
this:
PASSCHECK: κ[?] → κ7 ← {
LOWERS ← "abcdefghijklmnopqrstuvwxyz";
UPPERS ← "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NUMBERS ← "0123456789";
SPECIALS ← "!#$%&?~^-\|/";
⎕RETURNIF ← ⟨8 ≤ ρω, "E000000"⟩;
⎕RETURNIF ← ⟨¬/∨/∨ω = LOWERS, "E000001"⟩;
⎕RETURNIF ← ⟨¬/∨/∨ω = UPPERS, "E000002"⟩;
⎕RETURNIF ← ⟨¬/∨/∨ω = NUMBERS, "E000003"⟩;
⎕RETURNIF ← ⟨¬/∨/∨ω = SPECIALS, "E000004"⟩;
⎕RETURNIF ← ⟨¬/∧/∧ω = &/ LOWERS UPPERS NUMBERS SPECIALS, "E000005"⟩;
"SUCCESS"
};
PASSWORD ← ⎕:κ[?];
ERR ← ⎕PASSCHECK PASSWORD;
⎕ASSERT ← ⟨ERR = "SUCCESS", ⎕SWITCH:κ[?] ⟨
⟨ERR = "E000000", "PASSWORD IS TOO SHORT"⟩,
⟨ERR = "E000001", "PASSWORD DOES NOT CONTAIN ANY LOWERCASE LETTERS"⟩,
⟨ERR = "E000002", "PASSWORD DOES NOT CONTAIN ANY UPPERCASE LETTERS"⟩,
⟨ERR = "E000003", "PASSWORD DOES NOT CONTAIN ANY NUMBERS"⟩,
⟨ERR = "E000004", "PASSWORD DOES NOT CONTAIN ANY SPECIAL CHARACTERS"⟩,
⟨ERR = "E000005", "PASSWORD CONTAINS CHARACTERS THAT ARE NOT ALLOWED"⟩,
"OTHER ERROR"
⟩
⟩;
⎕ ← PASSWORD;
INPUT(κ[?]): password
ASSERTION ERROR: PASSWORD IS TOO SHORT
INPUT(κ[?]): apassword
ASSERTION ERROR: PASSWORD DOES NOT CONTAIN ANY UPPERCASE LETTERS
INPUT(κ[?]): APASSWORD
ASSERTION ERROR: PASSWORD DOES NOT CONTAIN ANY LOWERCASE LETTERS
INPUT(κ[?]): aPassword
ASSERTION ERROR: PASSWORD DOES NOT CONTAIN ANY NUMBERS
INPUT(κ[?]): aPassword0
ASSERTION ERROR: PASSWORD DOES NOT CONTAIN ANY SPECIAL CHARACTERS
INPUT(κ[?]): aPassword0!
aPassword0!
INPUT(κ[?]): aPassword0!⍤
ASSERTION ERROR: PASSWORD CONTAINS CHARACTERS THAT ARE NOT ALLOWED